<?php


namespace app\api\controller\v20;


use app\api\controller\v13\Auth2;
use app\common\cachemodel\MiniDateText;
use app\common\cachemodel\Recode;
use app\common\cachemodel\ZlAstroCalendarGoods;
use app\common\cachemodel\AstroChange as AstroChangeModel;
use app\common\lib\astrolog\Astrolog;
use app\common\model\UserPush;
use think\Env;

/**
 * @version 2.4.0
 * Class AstroCalendar
 * @package app\api\controller\v19
 */
class AstroCalendar extends Auth2
{
    /**
     * 获取天象详情
     * @return \think\response\Json
     * @throws \Exception
     */
    public function getDayInfo()
    {
        $date = input('date/s', '');
        $timezone = (isset($this->headers['timezone']) && $this->headers['timezone']) ? $this->headers['timezone'] : 8;

        if (!$date)
        {
            $date = date('Y-m-d H:i:s');
        }

        $intDate = (int)date('Ymd', strtotime($date));

        $astroChangeModel = new AstroChangeModel();
        // 获取星象变动数据 正在进行中的星象
        $underwayAstro = $astroChangeModel->getChangeDayList($date, $timezone);
        $astroChangeText = [];
        if ($this->user && $this->user['rid'])
        {
            $newIntDate = (int)date('Ymd'); // 今天的日期
            $isSendDingDing = $newIntDate === $intDate; // 是否发送钉钉消息

            $recodeInfo = (new Recode())->getInfoById($this->user['rid']);
            // 通过星象变动数据 获取星象变动词条
            $astroChangeText = $astroChangeModel->getChangeText($date, $timezone, $underwayAstro['listData'], $recodeInfo, $isSendDingDing);
        }

        if (!$astroChangeText)
        {   // 如果没有获取到星象变动词条 则使用 星座寄语 ->获取今天的星座寄语为词条
            // 因为2021年没有词条 所以每日一语词条使用2020年的
            if (date('Y', strtotime($date)) === '2021')
            {
                $intDate = (int)('2020' . date('md', strtotime($date)));
            }

            $word = (new MiniDateText())->getWord($intDate, 1);
            if (trim($word))
            {
                $astroChangeText[] = [
                    'title' => '每日一语',
                    'content' => $word,
                ];
            }
        }
        // 获取今日天象推荐的商品
        $goods = (new ZlAstroCalendarGoods())->getGoodsToDate($intDate);

        $goodsData = [];
        foreach ($goods as $goodItem)
        {
            $goodsData[] = [
                'good_img' => $goodItem['img'],
                'img_scale' => $goodItem['img_scale'],
                'target' => $goodItem['target'],
                'link' => $goodItem['link'],
            ];
        }

        $birthday_list = [];
        if ($this->user)
        {   // 已登陆
            $userPush = (new UserPush())->getUserRegid($this->user['id']);
            if (!isset($userPush['calendar_birthday_status']) || (int)$userPush['calendar_birthday_status'] !== 2)
            {
                $recodeData = (new Recode())->userBirthMonthList($this->user, $date, $timezone);
                $serverTime = gmdateModify('Y-m-d', time(), 8, (float)$timezone);//根据用户的时区进行转换的时间
                [$clientYear, $clientMonth, $clientday] = explode('-', $date);//客户端时间
                $host = Env::get('host.domain');
                foreach ($recodeData as $item)
                {
                    $avatar = '';
                    $text = '';
                    $target = null;
                    $link = null;
                    $btn_text = null;
                    $is_birth = false;
                    $recodeMonth = (int)$item['month'] . '-' . (int)$item['day'];
                    //判断档案是否是本年本月本日的生日，用来领取权益
                    if ($serverTime == $date && (int)$clientMonth . '-' . (int)$clientday == $recodeMonth)
                    {
                        $target = $item['target'];
                        $link = $host . $item['link'];
                        $btn_text = $item['btn_text'];
                    }
                    //判断档案是否是本月本日的生日，用来页面显示
                    if ((int)$clientMonth . '-' . (int)$clientday == $recodeMonth)
                    {
                        $avatar = $item['avatar'];
                        $text = $item['text'];
                        $is_birth = true;
                    }
                    if ($is_birth)
                    {
                        $birthday_list[] = [
                            'avatar' => $avatar,
                            'text' => $text,
                            'target' => $target,
                            'link' => $link,
                            'btn_text' => $btn_text,
                        ];
                    }
                }
            }
        }
        $timestamp = time();
        $min = date('i', $timestamp);
//        $min = date('s', $timestamp) > 0 ? $min + 1 : $min;
        //获取当前天象数据
        $nowTimeInfo = [
            'year' => date('Y', $timestamp),
            'month' => date('m', $timestamp),
            'day' => date('d', $timestamp),
            'hour' => date('H', $timestamp),
            'minute' => $min,
            'second' => 0,
            'is_summer' => 0,
            'timezone' => 8,
            'longitude' => 0,
            'latitude' => 0,
        ];
        // 获取行星运行状态信息
        $nowPlanetState = Astrolog::getInstance()->setExecPrefix(config('astrolog.prefix'))->setAdvance($nowTimeInfo)->getPlanetStateData();

        $nowAstro = [
            'title' => '当前天象 ' . gmdateModify('H:i', $timestamp, 8, $timezone, true),//根据用户的时区进行转换的时间
            'info_list' => $nowPlanetState,
            'target' => 'local',
            'link' => 'astrolable?chart=now&date=' . date('Y-m-d', $timestamp) . '&hour=' . $nowTimeInfo['hour'] . '&min=' . sprintf("%02d", $min) . '&need_auth=1&timezone=' . $timezone
        ];

        $resData = [
            'underway_astro' => $underwayAstro['resData'],
            'astro_info_list' => $astroChangeText,
            'goods_data' => $goodsData,
            'birthday_list' => $birthday_list,
            'now_astro' => $nowAstro
        ];

        return show(config('code.success'), '请求成功', $resData);
    }
}
